package org.example.api;

import org.example.dao.UserDao;
import org.example.model.User;
import org.example.util.WebUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/register")
@MultipartConfig
public class registerServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, Object> body = new HashMap<>();
        req.setCharacterEncoding("UTF-8");
        User user = WebUtil.deserialize(req, User.class);
        User query = UserDao.registerHasUser(user.getUsername());
        if(query != null) {
            body.put("ok", false);
            body.put("error", "账号重复");
        }else{
            int n = UserDao.registerNewUser(user);
            body.put("ok", true);
        }
        WebUtil.serialize(resp, body);
    }
}
